package com.haojiang.mh.remote.widget.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.haojiang.mh.R;

public class RingView extends View {

    private final int ringWidth;
    private Paint paint;
    private Context context;
    private int width;
    private int height;
    private float radius;

    public RingView(Context context) {
        this(context, null);
    }

    public RingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        this.paint = new Paint();
        this.paint.setAntiAlias(true); //消除锯齿
        this.paint.setStyle(Paint.Style.STROKE); //绘制空心圆
        this.paint.setColor(ContextCompat.getColor(context, R.color.color_gray_light));
        this.ringWidth = dip2px(context, 10);
        this.paint.setStrokeWidth(ringWidth);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height= h;
        if(width <= height ) {
            radius = width/2;
        }else  {
            radius = height/2;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

//        int innerCircle = dip2px(context, 50); //设置内圆半径
//        int ringWidth = dip2px(context, 5); //设置圆环宽度
//
//        //绘制内圆
//        this.paint.setARGB(155, 167, 190, 206);
//        this.paint.setStrokeWidth(2);
//        canvas.drawCircle(radius, radius, innerCircle, this.paint);
//
//        //绘制圆环
//        this.paint.setARGB(255, 212, 225, 233);
//        this.paint.setStrokeWidth(ringWidth);
//        canvas.drawCircle(radius, radius, innerCircle + 1 + ringWidth / 2, this.paint);
//
//        //绘制外圆
//        this.paint.setARGB(155, 167, 190, 206);
//        this.paint.setStrokeWidth(2);
//        canvas.drawCircle(radius, radius, innerCircle + ringWidth, this.paint);
        drawInnerCircle(canvas);

        super.onDraw(canvas);
    }

    private void drawInnerCircle(Canvas canvas) {
        canvas.save();
        try {
            canvas.translate(width/2, height/2);

            canvas.drawCircle(0, 0, radius-ringWidth, paint);
//            canvas.drawLine(-width/2, 0, width/2, 0, paint);
//            canvas.drawLine(0, -height/2, 0, height/2, paint);
        }finally {
            canvas.restore();
        }

    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
